# 辅助功能接口文件
import tkinter as tk
from threading import Thread
from Camera import Camera

debugMode = True

def debugLog(msg):
    if debugMode:
        print(msg)

def configWin(camera:Camera):
    if debugMode:
        t = Thread(target=_configWin, args=(camera,))
        t.start()
        return t
    return None

def _setCameraBCS(camera:Camera, brightness, contrast, saturation):
    camera.SetBrightness(brightness)
    camera.SetContrast(contrast)
    camera.SetSaturation(saturation)

def _configWin(camera:Camera):
    win1 = tk.Tk()
    win1.title("Config")
    win1.geometry("700x400")
    scint_1 = tk.IntVar(value=round(camera.brightness_gamma * 50))
    scint_2 = tk.IntVar(value=round(camera.contrast * 50))
    scint_3 = tk.IntVar(value=round(camera.saturation_gamma * 50))
    scint_4 = tk.IntVar(value=0)
    sbar_1 = tk.Scale(win1, from_=0, to=100, orient='horizontal', variable=scint_1, command=lambda z: _setCameraBCS(camera, scint_1.get() / 50.0, scint_2.get() / 50.0, scint_3.get() / 50.0))
    sbar_2 = tk.Scale(win1, from_=0, to=100, orient='horizontal', variable=scint_2, command=lambda z: _setCameraBCS(camera, scint_1.get() / 50.0, scint_2.get() / 50.0, scint_3.get() / 50.0))
    sbar_3 = tk.Scale(win1, from_=0, to=100, orient='horizontal', variable=scint_3, command=lambda z: _setCameraBCS(camera, scint_1.get() / 50.0, scint_2.get() / 50.0, scint_3.get() / 50.0))
    sbar_4 = tk.Scale(win1, from_=0, to=100, orient='horizontal', variable=scint_4, command=lambda z: camera.SetLedIntensity(round(scint_4.get() / 100.0 * 255.0)))
    sbar_1.grid(row=0, column=0)
    sbar_2.grid(row=1, column=0)
    sbar_3.grid(row=2, column=0)
    sbar_4.grid(row=3, column=0)
    win1.mainloop()
